package com.eshop.we.business;

import com.baomidou.mybatisplus.mapper.Wrapper;
import org.springframework.util.StringUtils;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.IService;
import javax.annotation.PostConstruct;
import java.io.Serializable;
import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * C/**
 * ${DESCRIPTION}
 *
 * @author wangzhanguo
 * @create 2018-07-05 11:13
 **/

public class BaseBusinessImpl<T,S extends IService<T>> implements BaseBusiness<T>{

    protected S service;
    @PostConstruct
    public void init() throws NoSuchFieldException, IllegalAccessException {
        Class clazz = this.getClass();
        Type type = clazz.getGenericSuperclass();
        ParameterizedType ptype = (ParameterizedType) type;
        Type[] types = ptype.getActualTypeArguments();
        Class serviceClass = (Class) types[1];
        Field[] fieldParameters = clazz.getDeclaredFields();
        for (Field field:fieldParameters) {
            if (field.getType().equals(serviceClass)){
                field.setAccessible(true);
                service = (S)field.get(this);
                break;
            }
        }
       // Field fieldParameters = clazz.getDeclaredField(StringUtils.uncapitalize(serviceClass.getSimpleName()));
//        fieldParameters.setAccessible(true);
        //service = (S)fieldParameters.get(this);
    }

    @Override
    public boolean insert(T var1) {
        return service.insert(var1);
    }

    @Override
    public boolean insertAllColumn(T var1) {
        return service.insertAllColumn(var1);
    }

    @Override
    public boolean insertBatch(List<T> var1) {
        return service.insertBatch(var1);
    }

    @Override
    public boolean insertBatch(List<T> var1, int var2) {
        return service.insertBatch(var1,var2);
    }

    @Override
    public boolean insertOrUpdateBatch(List<T> var1) {
        return service.insertOrUpdateBatch(var1);
    }

    @Override
    public boolean insertOrUpdateBatch(List<T> var1, int var2) {
        return service.insertOrUpdateBatch(var1,var2);
    }

    @Override
    public boolean insertOrUpdateAllColumnBatch(List<T> var1) {
        return service.insertOrUpdateAllColumnBatch(var1);
    }

    @Override
    public boolean insertOrUpdateAllColumnBatch(List<T> var1, int var2) {
        return service.insertOrUpdateAllColumnBatch(var1,var2);
    }

    @Override
    public boolean deleteById(Serializable var1) {
        return service.deleteById(var1);
    }

    @Override
    public boolean deleteByMap(Map<String, Object> var1) {
        return service.deleteByMap(var1);
    }

    @Override
    public boolean delete(Wrapper<T> wrapper) {
        return false;
    }

    @Override
    public boolean deleteBatchIds(Collection<? extends Serializable> var1) {
        return service.deleteBatchIds(var1);
    }

    @Override
    public boolean updateById(T var1) {
        return service.updateById(var1);
    }

    @Override
    public boolean updateAllColumnById(T var1) {
        return service.updateAllColumnById(var1);
    }

    @Override
    public boolean update(T t, Wrapper<T> wrapper) {
        return false;
    }

    @Override
    public boolean updateBatchById(List<T> var1) {
        return service.updateBatchById(var1);
    }

    @Override
    public boolean updateBatchById(List<T> var1, int var2) {
        return service.updateBatchById(var1,var2);
    }

    @Override
    public boolean updateAllColumnBatchById(List<T> var1) {
        return service.updateAllColumnBatchById(var1);
    }

    @Override
    public boolean updateAllColumnBatchById(List<T> var1, int var2) {
        return service.updateAllColumnBatchById(var1,var2);
    }

    @Override
    public boolean insertOrUpdate(T var1) {
        return service.insertOrUpdate(var1);
    }

    @Override
    public boolean insertOrUpdateAllColumn(T var1) {
        return service.insertOrUpdateAllColumn(var1);
    }

    @Override
    public T selectById(Serializable var1) {
        return service.selectById(var1);
    }

    @Override
    public List<T> selectBatchIds(Collection<? extends Serializable> var1) {
        return service.selectBatchIds(var1);
    }

    @Override
    public List<T> selectByMap(Map<String, Object> var1) {
        return service.selectByMap(var1);
    }

    @Override
    public T selectOne(Wrapper<T> wrapper) {
        return service.selectOne(wrapper);
    }

    @Override
    public Map<String, Object> selectMap(Wrapper<T> wrapper) {
        return service.selectMap(wrapper);
    }

    @Override
    public Object selectObj(Wrapper<T> wrapper) {
        return service.selectObj(wrapper);
    }

    @Override
    public int selectCount(Wrapper<T> wrapper) {
        return service.selectCount(wrapper);
    }

    @Override
    public List<T> selectList(Wrapper<T> wrapper) {
        return service.selectList(wrapper);
    }

    @Override
    public Page<T> selectPage(Page<T> var1) {
        return service.selectPage(var1);
    }

    @Override
    public List<Map<String, Object>> selectMaps(Wrapper<T> wrapper) {
        return service.selectMaps(wrapper);
    }

    @Override
    public List<Object> selectObjs(Wrapper<T> wrapper) {
        return service.selectObjs(wrapper);
    }

    @Override
    public Page<Map<String, Object>> selectMapsPage(Page page, Wrapper<T> wrapper) {
        return service.selectMapsPage(page,wrapper);
    }

    @Override
    public Page<T> selectPage(Page<T> page, Wrapper<T> wrapper) {
        return service.selectPage(page,wrapper);
    }
}

